﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharpPad.Forms
{
    public partial class ManageNamespaces : SharpPad.Forms.ModalDialog
    {
        public List<String> Namespaces { get; set; }

        public ManageNamespaces():this(new List<string>())
        {}

        public ManageNamespaces(IEnumerable<String> namespaces)
        {
            InitializeComponent();
            Namespaces = namespaces.ToList();
            RefreshList();
        }

        private void AddClick(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(NewNamespace.Text)) return;

            // TODO.ZJG: Need to do input validation here.
            Namespaces.Add(NewNamespace.Text);
            RefreshList();
            NewNamespace.ResetText();
        }

        private void RemoveClick(object sender, EventArgs e)
        {
            if (NamespaceList.SelectedItems.Count == 0) return;
            foreach (ListViewItem item in NamespaceList.SelectedItems)
            {
                Namespaces.Remove(item.Text);
                item.Remove();
            }
            RefreshList();
        }

        private void RefreshList()
        {
            if (Namespaces.Count == 0) return;

            NamespaceList.SuspendLayout();
            NamespaceList.Items.Clear();

            foreach (var @namespace in Namespaces)
            {
                NamespaceList.Items.Add(@namespace);
            }

            NamespaceList.Columns[0].Width = -2; // Autowidth

            NamespaceList.ResumeLayout(true);
        }

    }
}
